﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ServerSide.Common;
using ServerSide.EFCoreContext;
using ServerSide.Model;
using ServerSide.Model.Dto.RoleDto;
using ServerSide.Model.Other;
using ServerSide.Service.Interface;

namespace ServerSide.Controllers
{
    [ApiController]
    [Route("roleapi/[action]")]
    public class RoleController
    {
        private readonly MyDBContext _myDBContext;
        private readonly IRoleService _roleService;
        public RoleController(MyDBContext myDBContext,IRoleService roleService)
        {
            _myDBContext = myDBContext;
            _roleService = roleService;
        }
        /// <summary>
        /// 角色管理页面-获取角色数据
        /// </summary>
        /// <param name="roleRequest"></param>
        /// <returns></returns>
        [HttpPost(Name = "getRoleData")]
        public Result<Object> GetRoleData(RoleRequest roleRequest)
        {
            PageInfo pageInfo = _roleService.GetRole(roleRequest);
            if (pageInfo != null)
            {
                return Result<Object>.Success(pageInfo);
            }
            return Result<Object>.Error("501","获取失败");
        }

        /// <summary>
        /// 新增、编辑时通过用户id获取角色信息，提供用户选择
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet(Name = "getRoleList")]
        public Result<Object> GetRoleList(int? userId)
        {
            //新增时直接获取所有角色信息数据
            List<Role> roles = _roleService.GetRoleList();
            if (roles != null)
            {
                if (userId != null)
                {
                    //编辑时获取所有
                    List<int> roleIds = _roleService.GetRoleListByUId(userId);
                    return Result<Object>.Success(new
                    {
                        roles,
                        roleIds
                    });
                }
                else
                {
                    return Result<Object>.Success(roles);
                }
            }
            return Result<Object>.Error("501", "获取失败");
        }
        /// <summary>
        /// 角色管理页面-添加角色信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost(Name = "addRole")]
        public Result<Object> AddRole(Role role)
        {
            bool result = _roleService.AddRole(role);
            if (result)
            {
                return Result<Object>.Success("添加成功");
            }
            return Result<Object>.Error("501", "添加失败");
        }

        /// <summary>
        /// 角色管理页面-删除角色信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost(Name = "deleteRole")]
        public Result<Object> DeleteRole(Role role)
        {
            bool result = _roleService.DelRole(role);
            if (result)
            {
                return Result<Object>.Success("删除角色成功");
            }
            return Result<Object>.Error("501", "删除角色失败");
        }

        /// <summary>
        /// 角色管理页面-批量删除角色信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost(Name = "batchDelRole")]
        public Result<Object> BatchDelRole(Object obj)
        {
            List<int>? ids = JsonConvert.DeserializeObject<List<int>>(obj.ToString());
            bool result = _roleService.BatchDelRoles(ids);
            if (result)
            {
                return Result<Object>.Success("批量删除角色成功");
            }
            return Result<Object>.Error("501", "批量删除角色失败");
        }

        /// <summary>
        /// 角色管理页面-修改角色信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost(Name = "updateRoleData")]
        public Result<Object> UpdateRoleData(Role role)
        {
            bool result = _roleService.UpdateRole(role);
            if (result)
            {
                return Result<Object>.Success("修改角色成功");
            }
            return Result<Object>.Error("501", "修改角色失败");
        }

        /// <summary>
        /// 角色管理页面-修改角色启用禁用信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpGet(Name = "changeRoleEnable")]
        public Result<Object> ChangeRoleEnable(int id, int enable)
        {
            bool v = _roleService.UpdateRoleEnable(id, enable);
            if (v)
            {
                return Result<Object>.Success("修改角色状态成功");
            }
            return Result<Object>.Error("501", "修改角色状态失败");
        }

        /// <summary>
        /// 角色管理页面-分配角色权限
        /// </summary>
        /// <param name="roleMenu"></param>
        /// <returns></returns>
        [HttpPost(Name = "updataRoleMenuScope")]
        public Result<Object> UpdataRoleMenuScope(RoleMenuRequest roleMenuRequest)
        {
            bool v = _roleService.UpdataRoleMenuScope(roleMenuRequest);
            if (v)
            {
                return Result<Object>.Success("权限分配成功");
            }
            return Result<Object>.Error("501", "权限分配失败");
        }
    }
}
